package com.hutao.manage.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hutao.manage.datasource.TargetDataSource;
import com.hutao.manage.mapper.MedicalResourceMapper;
import com.hutao.manage.mapper.MedicalResourcePicMapper;
import com.hutao.manage.pojo.MedicalResource;
import com.hutao.manage.service.MedicalResourceService;

@Service
public class MedicalResourceServiceImpl implements MedicalResourceService {

	@Autowired
	private MedicalResourceMapper medicalResourceMapper;
	
	@Autowired
	private MedicalResourcePicMapper medicalResourcePicMapper;
	
	@TargetDataSource(name="ds1")
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public int insert(MedicalResource medicalResource) {
		// TODO Auto-generated method stub
	
		return medicalResourceMapper.insert(medicalResource);
	}

	@TargetDataSource(name="ds1")
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public int deleteCertainPageTypeImgs(String firstGuid, Integer pageTypeId) {
		List<Map<String, Object>> medResourceList = medicalResourceMapper.getMedResourceByFirstGuidAndPageTypeId(firstGuid, pageTypeId);
		if (null != medResourceList && medResourceList.size() > 0) {
			for (int i = 0; i < medResourceList.size(); i++) {
				Map <String, Object> medResourceMap = medResourceList.get(i);
				Integer medResourceId = medResourceMap.get("id")!=null?(Integer)medResourceMap.get("id"):null;
				medicalResourcePicMapper.deleteByMedicalResourceId(medResourceId);
				medicalResourceMapper.deleteByPrimaryKey(medResourceId);
			}
			
		}
		
		return 1;
	}

}
